<?php 
/**
 * 商品控制器
 * @author 杨帆
 */
class GoodsController extends AuthController{
    private $model;
    private $modelAt;
    private $modelEn;
    /**
     * 自动执行方法，应用的是框架的自动执行方法;
     */
    public function __init(){
        //每次运行控制器都会打开扩展模型'Flink';
        $this->model = K('Goods');
        $this->modelAt = K('GoodsAttr');
        $this->modelEn = K('GoodsEntry');
    } 
    
    /**
     * 默认显示
     */
    public function index(){
       //分页
       //数据总数
       $total = $this->model->count(); 
       //1.总数 2.每页显示的文章数 3.显示的页面数量
       $pageObj = new Page($total,6,3);
       //显示分页,show为方法;
       $page = $pageObj->show(2);
       //分配数据
       $this->assign('page',$page);
       //分配数据 
       $goods = $this->model->limit($pageObj->limit())->all();
//     p($goods);die;
       $this->assign('goods',$goods);
       //显示视图
       $this->display();
    }
    
    /**
     * 添加
     */
    public function add(){
        if(IS_POST){
            //执行全部表格的验证与添加操作
            if(!$this->model->addData()){
                $this->error($this->model->error);
            }
            //成功提示
            $this->success('添加成功',U('index'));
        }
        //查询分类数据
        $class = M('class')->all();
        //框架数据书方法;
        $class = Data::tree($class,'name','cid','fid');
        $this->assign('class',$class);
        //查询品牌数据
        $brand = M('brand')->all();
        $this->assign('brand',$brand);
        $this->display(); 
    }
    
    /**
     * 添加
     */
    public function edit(){
        if(IS_POST){
//          p($_POST);die;
            //隐藏域的值
            //这个隐藏域就是为了让框架认为是编辑操作
            //因为自动完成sendtime修改的时候不能变
            $gid = Q('post.gid',0,'intval');
            if(!$this->model->editData($gid)){
                $this->error($this->model->error);
            }
              $this->success('修改成功',U('index'));
        }
        
        $gid = Q('get.gid',0,'intval');
        //获得商品主表旧数据
        $oldData = M('goods')->where("gid={$gid}")->find();
        $this->assign('oldData',$oldData);
        //获取商品明细表旧数据
        $oldData1 = M('goods_entry')->where("goods_gid={$gid}")->find();
        $this->assign('oldData1',$oldData1);
        //获取商品明细表列表图旧数据
        $oldData2 = M('goods_entry')->where("goods_gid={$gid}")->getfield('shop_pic');
        $oldData2 = unserialize($oldData2);
        $this->assign('oldData2',$oldData2);
        //处理分类数据
        $class = M('class')->all();
        $class = Data::tree($class,'name','cid','fid');
        $this->assign('class',$class);
        //处理品牌数据
        $brand = M('brand')->all();
        $this->assign('brand',$brand);
        //读取缓存
        $selected = S('selected_' . $gid);
        $this->assign('selected',$selected);
        $this->display();     
    }
    /**
     * 异步通过cid获得类型属性
     */
    public function getTypeAttr(){
        if(!IS_AJAX) return;
        $cid = Q('post.cid',0,'intval');
        $data = M()->join('__attribute__ at JOIN __class__ c ON at.type_tid=c.type_tid')->where("c.cid={$cid}")->all();
        //把类型属性的值按照,拆分
        foreach ($data as $k => $v) {
            $data[$k]['content'] = explode(',', $v['content']);
        }
        echo json_encode($data);
        exit;
    }
    
    /**
     * 列表图上传
     */
    public function uploadList(){
        $upload = new Upload('Upload/Content/list/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
                //图片路径
            $data = $file[0];
            $this->ajax($data);
        }
    }
    
    /*
     * 商品图册上传
     */
    public function uploadPic(){
        $upload = new Upload('Upload/Content/shop/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
            //上传成功
            $type = strrchr($file[0]['path'], '.');
            //缩略操作
            $img = new Image();
            //缩略小图
            $smallPath = str_replace($type, '_small' . $type, $file[0]['path']);
            $img->thumb($file[0]['path'],$smallPath,200,200,2);
            //缩略中图
            $midPath = str_replace($type, '_mid' . $type, $file[0]['path']);
            $img->thumb($file[0]['path'],$midPath,400,400,2);
            //缩略大图
            $bigPath = str_replace($type, '_big' . $type, $file[0]['path']);
            $img->thumb($file[0]['path'],$bigPath,800,800,2);
            //图片路径
            $data = $file[0];
            $this->ajax($data);
        }
    }
    
    /**
     * 删除商品
     */
    public function del(){
        //删除操作
        //获取元素主键id(cid);
        $gid = Q('get.gid',0,'intval');
        //如果post中传过来的数据为空,默认给一个空数组.防止数据报错.
        if(!$gid){
            //如果为空数组,则提示请选择再删除;
            $this->error('请选择再删除');
        }
        //删除主表数据;
        $this->model->where('gid='. $gid)->delete();
        //删除明细表数据;
        $this->modelEn->where('goods_gid='. $gid)->delete();
        //删除属性表数据;
        $this->modelAt->where('goods_gid='. $gid)->delete();
        //提示信息;
        $this->success('删除成功');
    }
    
    /**
     * 删除列表图片
     */
    public function delImg(){
       if(IS_AJAX){
          //删除缩略图
          $path = Q('post.path');
          $gid = Q('post.gid',0,'intval');
          //判断$path是不是一个文件，是就删除目录
          if(is_file($path)) unlink($path);
          //删除原图
          $path = str_replace(C('THUMB_ENDFIX'),'',$path);
          //判断原图是不是一个目录，是就删除
          if(is_file($path)) unlink($path);
          //删除对应文章的图片信息
          $this->model->where("gid={$gid}")->update(array('pic'=>''));
       }
    } 
    /**
     * 删除商品图片
     */
    public function delPic(){
        if(IS_AJAX){
            //删除图片
            $path = Q('post.path');
            //获得类型
            $type = strrchr($path, '.');
            //小图
            $smallPath = str_replace($type, '_small' . $type, $path);
            if(is_file($smallPath)) unlink($smallPath);
            //中图
            $midPath = str_replace($type, '_mid' . $type, $path);
            if(is_file($midPath)) unlink($midPath);
            //大图
            $bigPath = str_replace($type, '_big' . $type, $path);   
            if(is_file($bigPath)) unlink($bigPath);
            //上传图
            $thumbPath = str_replace($type, '_thumb' . $type, $path);   
            if(is_file($thumbPath)) unlink($thumbPath); 
            //删除本图片         
            if(is_file($path)) unlink($path);
            //删除对应文章的图片信息
            $this->modelEn->where("goods_gid={$gid}")->update(array('shop_pic'=>''));
        }    
    }    
}